//
//  ViewController.m
//  DrawingViewDemo
//
//  Created by xxss0903 on 16/3/17.
//  Copyright © 2016年 rry.com. All rights reserved.
//

#import "ViewController.h"
#import "DrawingTool.h"

typedef enum : NSUInteger {
    kColorSheet,
    kToolSheet,
} sheetType;

@interface ViewController () <UIActionSheetDelegate, DrawingViewDelegate, DrawingToolDelegate>

@property (weak, nonatomic) IBOutlet UISlider *widthSlider;
@property (weak, nonatomic) IBOutlet UISlider *alphaSlider;

@end

@implementation ViewController

#pragma mark - 自定义方法
#pragma mark 按钮点击方法
- (IBAction)colorsBtnClicked:(id)sender {
    UIActionSheet *colorsSheet = [[UIActionSheet alloc] initWithTitle:@"请选择颜色" delegate:self
                                                    cancelButtonTitle:@"取消"
                                               destructiveButtonTitle:nil
                                                    otherButtonTitles:@"红色", @"蓝色", @"绿色"
                                                    , @"黄色", @"黑色", @"橙色", nil];
    colorsSheet.tag = kColorSheet;
    colorsSheet.delegate = self;
    [colorsSheet showInView:self.view];
}

- (IBAction)toolsBtnClicked:(id)sender {
    UIActionSheet *toolsSheet = [[UIActionSheet alloc] initWithTitle:@"请选择工具" delegate:self
                                                    cancelButtonTitle:@"取消"
                                               destructiveButtonTitle:nil
                                                    otherButtonTitles:@"画笔", @"直线", @"矩形", @"矩形（填充）"
                                  , @"圆形", @"圆形（填充）", @"橡皮擦", @"箭头",
                                 @"文本", @"富文本" ,nil];
    toolsSheet.tag = kToolSheet;
    toolsSheet.delegate = self;
    [toolsSheet showInView:self.view];
}

- (IBAction)widthBtnClicked:(id)sender {
    self.widthSlider.hidden = !self.widthSlider.hidden;
    self.alphaSlider.hidden = YES;
}

- (IBAction)alphaBtnClicked:(id)sender {
    self.alphaSlider.hidden = !self.alphaSlider.hidden;
    self.widthSlider.hidden = YES;
}

- (IBAction)undoBtnClicked:(id)sender {
    [self.drawingView undoLastObject];
    [self updateButtonStatus];

}

- (IBAction)redoBtnClicked:(id)sender {
    [self.drawingView redoLastObject];
    [self updateButtonStatus];
}

- (IBAction)clearBtnClicked:(id)sender {
    [self.drawingView clearScreen];
}

- (IBAction)screenShotBtnClicked:(id)sender {
}

- (IBAction)widthSliderValueChanged:(id)sender {
    self.drawingView.lineWidth = self.widthSlider.value;
}

- (IBAction)alphaSliderValueChanged:(id)sender {
    self.drawingView.lineAlpha = self.alphaSlider.value;
}


// 更新按钮状态
- (void)updateButtonStatus
{
    self.redoBtn.enabled = [self.drawingView canredo];
    self.undoBtn.enabled = [self.drawingView canundo];
}

// 初始化参数
- (void)setupInitialValue
{
    
}

#pragma mark - 代理方法
#pragma mark 自定义代理方法
- (void)drawingViewDidFinishedDraw:(DrawingView *)drawingView
{
    [self updateButtonStatus];
}

#pragma mark actionsheet代理方法
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (actionSheet.cancelButtonIndex == buttonIndex) {
        return;
    }
    
    if (actionSheet.tag == kColorSheet) {
        self.colorsBtn.title = [actionSheet buttonTitleAtIndex:buttonIndex];
        switch (buttonIndex) {
            case 0:
                self.drawingView.lineColor = [UIColor redColor];
                break;
            case 1:
                self.drawingView.lineColor = [UIColor blueColor];
                break;
            case 2:
                self.drawingView.lineColor = [UIColor greenColor];
                break;
            case 3:
                self.drawingView.lineColor = [UIColor yellowColor];
                break;
            case 4:
                self.drawingView.lineColor = [UIColor blackColor];
                break;
            case 5:
                self.drawingView.lineColor = [UIColor orangeColor];
                break;
                
            default:
                break;
            }
        } else if (actionSheet.tag == kToolSheet) {
            self.toolsBtn.title = [actionSheet buttonTitleAtIndex:buttonIndex];
            switch (buttonIndex) {
                case 0:
                    self.drawingView.drawToolType = DrawingToolTypePen;
                    break;
                case 1:
                    self.drawingView.drawToolType = DrawingToolTypeLine;
                    break;
                case 2:
                    self.drawingView.drawToolType = DrawingToolTypeRectangle;
                    break;
                case 3:
                    self.drawingView.drawToolType = DrawingToolTypeRectangleFill;
                    break;
                case 4:
                    self.drawingView.drawToolType = DrawingToolTypeOval;
                    break;
                case 5:
                    self.drawingView.drawToolType = DrawingToolTypeOvalFill;
                    break;
                case 6:
                    self.drawingView.drawToolType = DrawingToolTypeEraser;
                    break;
                case 7:
                    self.drawingView.drawToolType = DrawingToolTypeArrow;
                    break;
                case 8:
                    self.drawingView.drawToolType = DrawingToolTypeText;
                    break;
                case 9:
                    self.drawingView.drawToolType = DrawingToolTypeMultiText;
                    break;
                default:
                    break;
            }
    }
}

#pragma mark - 系统方法
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.drawingView.delegate = self;
    [self updateButtonStatus];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
